<?php
declare(strict_types=1);
namespace App\Service;

use App\Constants\ErrorCode;
use App\Dao\ArticleDao;
use App\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use App\Dao\AdminLogDao;

class ArticleService
{
    /**
     * @Inject
     * @var ArticleDao
     */
    protected $dao;

    /**
     * @Inject
     * @var AdminLogDao
     */
    protected $logdao;

    /**
     * 资讯编辑保存
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function editSave($request){
        $request['update_time'] = time();
        $result = $this->dao->editOne(['id'=>$request['id']],$request);
        if(!$result) throw new BusinessException(ErrorCode::ERR_BUESSUS,'资讯编辑失败');
        return $result;
    }

    /**
     * 资讯编辑
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function editShow($id){
        $result = $this->dao->searchOne(['id'=>$id]);
        if(!$result) throw new BusinessException(ErrorCode::ERR_BUESSUS,'资讯编辑查询失败');
        return $result;
    }

    /**
     * 资讯新增
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function add($request){
        $request['add_time'] = $request['update_time']  = time();
        $result = $this->dao->addArticle($request);
        if(!$result) throw new BusinessException(ErrorCode::ERR_BUESSUS,'资讯新增失败');
        $this->logdao->addLog('新增了资讯');
        return $result;
    }

    /**
     * 资讯列表
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function list($page,$pageSize,$order){
        $where[] = ['is_del','=' ,1];
        return $this->dao->searchByWherePage((int)$pageSize,(int)$page,$where,$order);
    }

    /**
     * 小程序资讯列表API
     * @Author abell
     * @DateTime 2020-06-16
     * @param array $request
     * @return
     */
    public function listApi($page,$pageSize,$order){
        $where = ['is_del'=>1,'status'=>1];
        return $this->dao->searchByWherePage((int)$pageSize,(int)$page,$where,$order);
    }
}